package com.icangya.interceptor;

import com.icangya.Utils.CusAccessObjectUtil;
import com.icangya.Utils.FormatUtil;
import com.icangya.Value.FootBall;
import com.icangya.domain.FBIpRequest;
import com.icangya.domain.User;
import com.icangya.service.IBadUserService;
import com.icangya.service.ISystemService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Created by 王小红 on 2017/5/16.
 */
public class RequestCountInterceptor extends HandlerInterceptorAdapter {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private ISystemService iSystemService;
    @Autowired
    private IBadUserService iBadUserService;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {

        /** 拦截到用户的请求，判断是否重复，暴力请求 */
        User user = (User) request.getSession().getAttribute(FootBall.FOOT_USER_SESSION);
        String createTime = String.valueOf(new Date().getTime());
        String ip = CusAccessObjectUtil.getIpAddress(request);
        if (user!=null) {

            FBIpRequest fbIpRequest = new FBIpRequest();
            fbIpRequest.setCreateTime(FormatUtil.stampToDate(createTime));
            fbIpRequest.setIp(ip);
            fbIpRequest.setHeadImgUrl(user.getHeadImgUrl());
            fbIpRequest.setNickName(user.getNickName());
            fbIpRequest.setOpenid(user.getOpenId());
            fbIpRequest.setRemark("有用户信息的请求");
            fbIpRequest.setAll_ip(request.getRequestURI());
            iSystemService.save(fbIpRequest);

        }else{
            FBIpRequest fbIpRequest = new FBIpRequest();
            fbIpRequest.setCreateTime(FormatUtil.stampToDate(createTime));
            fbIpRequest.setIp(ip);
//            fbIpRequest.setHeadImgUrl(user.getHeadImgUrl());
//            fbIpRequest.setNickName(user.getNickName());
//            fbIpRequest.setOpenid(user.getOpenId());
            fbIpRequest.setAll_ip(request.getRequestURI());
            fbIpRequest.setRemark("无用户信息的请求");
            iSystemService.save(fbIpRequest);
        }

        List<FBIpRequest> fbIpRequestList = iSystemService.selectIpRequestByIpAndCreateTime(CusAccessObjectUtil.getIpAddress(request), FormatUtil.stampToDate(createTime));
        boolean flag = fbIpRequestList.size() > 60 ? true : false;
        if (flag) {//如果同一秒请求超过20次，则判定该用户的操作是非法的，执行拉黑
            logger.info("是否同一秒请求超过20次："+flag);
            if (user==null){
                iBadUserService.addBadUser(ip, "同一秒请求超过20次,由于用户信息为空,记录信息为其IP地址");
            }else{
                iBadUserService.addBadUser(user.getOpenId(), "同一秒请求超过20次");
            }


            return false;
        }
        return true;

//        String requestType = request.getHeader("X-Requested-With");
//        logger.info(requestType);
            /** */
//        if (requestType==null){
//            response.sendRedirect("https://baike.baidu.com/item/%E7%A5%9E%E7%BB%8F%E7%97%85");
//            return false;
//        }else{
//            logger.info(requestType);
//            return true;
//        }


    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }


}
